//package com.chenfan.filestore.job;
//
//import cn.hutool.core.util.NumberUtil;
//import com.chenfan.filestore.model.Task;
//import com.chenfan.filestore.mq.task.TaskConsumer;
//import com.chenfan.filestore.service.TaskService;
//import com.chenfan.infra.context.TenantContextHolder;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.annotation.XxlJob;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.*;
//
///**
// * @author K
// * @date 2023/5/18 2:33 PM
// */
//@Slf4j
//@Component
//public class TriggerTaskJob {
//
//    @Autowired
//    private TaskConsumer taskConsumer;
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
//
//    @XxlJob("triggerTask")
//    public ReturnT<String> triggerTask(String param) {
//        Integer taskLimitation = NumberUtil.isInteger(param) ? Integer.valueOf(param) : 50;
//        // 设置默认的参数.
//        TenantContextHolder.setTenantId(1L);
//        List<Task> tasks = taskService.listBySize(taskLimitation);
//        if (CollectionUtils.isEmpty(tasks)) {
//            return ReturnT.SUCCESS;
//        }
//
//        CompletionService<Boolean> completionService = new ExecutorCompletionService<>(threadPoolTaskExecutor);
//
//        List<Future<Boolean>> taskList = new ArrayList<>();
//
//        for(Task task: tasks) {
//            Future<Boolean> submit = completionService.submit(new TriggerTask(task.getTaskId(), taskConsumer));
//            taskList.add(submit);
//        }
//        // 并发调用
//        taskList.forEach(e -> {
//            try {
//                e.get();
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            } catch (ExecutionException ex) {
//                ex.printStackTrace();
//            }
//        });
//        return ReturnT.SUCCESS;
//    }
//
//    static class TriggerTask implements Callable<Boolean> {
//        private Long taskId;
//        private TaskConsumer taskConsumer;
//
//        public TriggerTask(Long taskId, TaskConsumer taskConsumer) {
//            this.taskConsumer = taskConsumer;
//            this.taskId = taskId;
//        }
//
//        @Override
//        public Boolean call() {
//            log.info("触发导入导出任务:{}", taskId);
//            taskConsumer.triggerTask(taskId);
//            return true;
//        }
//    }
//}
